<template>
  <a-card :bordered="false" class="card-area">
    <div>
      <a-form layout="horizontal">
        <div>
          <a-row>
            <a-col :md="6" :sm="24">
              <div class="operator">
                <a-button type="primary" ghost @click="add">新增</a-button>
                <a-button @click="batchDelete">删除</a-button>
              </div>
            </a-col>
            <a-col :md="6" :sm="24"/>
            <a-col :md="6" :sm="24"/>
            <a-col :md="6" :sm="30">
              <a-input-search placeholder="输入业务模型名称" enter-button="查找"
                              @search="search" v-model="queryParams.modelName"
                              :allowClear="true" />
            </a-col>
          </a-row>
        </div>
      </a-form>
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="record => record.modelId"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               @change="handleTableChange"
               :scroll="{ x: 900 }">
        <template slot="remark" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div style="max-width: 200px">{{text}}</div>
            </template>
            <p style="width: 200px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
        <template slot="operation" slot-scope="text, record">
          <a-icon type="setting" theme="twoTone" twoToneColor="#4a9ff5"
                  @click="edit(record)" title="修改配置"></a-icon>
        </template>
      </a-table>
      <addBizModel ref="AddBizModel" @close="handleAddClose"></addBizModel>
    </div>
  </a-card>
</template>

<script>
import AddBizModel from './AddBizModel'
export default {
  name: 'BizModel',
  components: {AddBizModel},
  data () {
    return {
      dataSource: [],
      selectedRowKeys: [],
      paginationInfo: null,
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
      queryParams: {},
      modelAddVisible: false,
      modelEditVisible: false,
      loading: false
    }
  },
  computed: {
    columns () {
      return [{
        title: '模型ID',
        dataIndex: 'modelId'
      }, {
        title: '模型名称',
        dataIndex: 'modelName'
      }, {
        title: '查询权限类型',
        dataIndex: 'authType'
      }, {
        title: '配置',
        dataIndex: 'operation',
        scopedSlots: {customRender: 'operation'},
        fixed: 'right',
        width: 100
      }]
    }
  },
  mounted () {
    this.fetch()
  },
  methods: {
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleAddClose () {
      this.search()
    },
    add () {
      this.$refs.AddBizModel.show()
    },
    edit (record) {
      this.$get('bizModel/detail/' + record.modelId).then((r) => {
        this.$refs.AddBizModel.setEditData(r.data.bizCfgModel, r.data.fieldData)
      })
      this.$refs.AddBizModel.show()
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的记录')
        return
      }
      let that = this
      this.$confirm({
        title: '确定删除所选中的记录?',
        content: '当您点击确定按钮后，业务模型将会被彻底删除',
        centered: true,
        onOk () {
          let dictIds = that.selectedRowKeys.join(',')
          that.$delete('bizModel/delete/' + dictIds).then(() => {
            that.$message.success('删除成功')
            that.selectedRowKeys = []
            that.search()
          })
        },
        onCancel () {
          that.selectedRowKeys = []
        }
      })
    },
    search () {
      this.fetch({
        ...this.queryParams
      })
    },
    reset () {
      // 取消选中
      this.selectedRowKeys = []
      // 重置分页
      this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
      if (this.paginationInfo) {
        this.paginationInfo.current = this.pagination.defaultCurrent
        this.paginationInfo.pageSize = this.pagination.defaultPageSize
      }
      this.paginationInfo = null
      // 重置查询参数
      this.queryParams = {}
      this.fetch()
    },
    handleTableChange (pagination, filters, sorter) {
      this.paginationInfo = pagination
      this.fetch({
        ...this.queryParams
      })
    },
    fetch (params = {}) {
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        params.pageNum = this.paginationInfo.current
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      this.$get('/bizModel/list', {
        ...params
      }).then((r) => {
        let data = r.data
        const pagination = { ...this.pagination }
        pagination.total = data.total
        this.loading = false
        this.dataSource = data.rows
        this.pagination = pagination
      })
    }
  }
}
</script>

<style lang="less" scoped>
  @import "../../../../static/less/Common";
</style>
